<template>
  <div class="tobar">
    <!-- 版心 -->
    <div class="wrap tobarwrap">
      <!-- 左侧标题 -->
      <div class="left">欢迎来到叩丁狼积分商场</div>
      <!-- 右侧标题 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" width="23px" />用户名:{{
            userInfo.nickName
          }}
        </li>
        <li>我的积分:{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" v-else @click="changeIsShowLoginModal(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState ,mapActions} from "vuex";

// 引入微信扫码登录APi weiXinLoginAPI
import{weiXinLoginAPI} from '@/requset/api';
export default {
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined:'loginStatus/changeIsLogined',
      initUserInfo:'userInfo/initUserInfo'
    }),
    ...mapActions({
      asyncChangeIsShowToast:'showToast/asyncChangeIsShowToast',
      asyncChangeUserInfo:'userInfo/asyncChangeUserInfo',
      
    })
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      cartTotal:(state)=>state.userInfo.cartTotal,
      userInfo:(state)=>state.userInfo.userInfo,
    }),
  },
  async created(){
    document.documentElement.scrollTop=0;
    let code = this.$route.query.code;
    if(code){
      //找后台去微信换取token
      let res = await weiXinLoginAPI({
        code,
      })
      // console.log('微信登录后res:',res)

      if(res.code == 0){
          //1 code == 0,登录成功
          //1.1提示
          alert('扫码登录成功');
          //1.2 存token
          localStorage.setItem('x-auth-token',res['x-auth-token']);
          //1.3 更新右上角购物车按钮提示
          this.changeIsShowLoginModal(false);
          this.changeIsLogined(true);
          //1.4清除地址栏中的code
          this.$route.query.code && this.$router.push(this.$route.path)

          //获取用户信息
          this.asyncChangeUserInfo()
      }else if(res.code == 400){
        //2.1 code过期的情况
        //提示失效
        this.asyncChangeIsShowToast({
          msg:'二维码已经失效,请重新扫描登录',
          type:'danger',
        })
        // 把登录模态窗口打开
        this.changeIsShowLoginModal(true);
      } else if(res.code == 407){
          //用户的微信还没有在这个网站上绑定过手机
          //提示绑定 
          this.asyncChangeIsShowToast({
            msg:'请使用手机号码绑定登录微信',
            type:'warning'
          })
          //把登录模态窗口打开
          this.asyncChangeIsShowToast(true);
           //保存uuid
          localStorage.setItem('uuid',res.uuid)
      }
    }else{
      let token = localStorage.getItem('x-auth-token');
      this.changeIsLogined(Boolean(token));

      if(token){
        this.asyncChangeUserInfo()
      } else{
        this.initUserInfo()
      }
    }
  },
  // watch:{
  //   // 监听路由的变化
  //   '$route.path':{
  //     handler(newVal,oldVal){
  //       if(newVal != oldVal){
  //         let token = localStorage.getItem("x-auth-token");
  //         //  if(token){
  //         //   this.changeIsLogined(true);
  //         //  }else{
  //         //   this.changeIsLogined(false);
  //         //  }
  //         //  this.changeIsLogined(Boolean(token))
  //       }
  //     }
  //   }
  // }
};
</script>

<style lang='less' scoped>
.tobar {
  background: #242b39;
  height: 40px;
  line-height: 40px;
  color: #fff;
  font-size: 14px;
  font-family: SourceHanSansSC;
  .tobarwrap {
    display: flex;
    justify-content: space-between;
    .right {
      display: flex;
      justify-content: space-between;
      li {
        cursor: pointer;
        margin-left: 20px;
        &.last {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
      img {
        vertical-align: middle;
        border-radius: 50%;
      }
    }
  }
}
</style>